package workexp.alg.question;

import org.junit.Test;

import java.util.Arrays;

//反转法最简单
public class Rotate {
    //error
    public void rotate2(int[] nums, int k) {
        if(nums.length<k){
            return ;
        }
        int len =nums.length;
        int right = len-1;
        int left = len-k-1;
        int[] tmp = new int[k];
        for (int i = len-k,j=0; i <len ; j++,i++) {
            tmp[j]=nums[i];
        }

        while (right>=0){
            if(right<k){
                int tmpoint = k-right-1;
                nums[tmpoint]=tmp[tmpoint];
            }else{
                nums[right]=nums[left];
            }
            left--;
            right--;
        }
    }

    public void rotate(int[] nums, int k) {
        int[] newarry = new int[nums.length];
        for (int i = 0; i < nums.length; i++) {
            newarry[(i+k)%nums.length]=nums[i];
        }

        System.arraycopy(newarry,0,nums,0,nums.length);
    }

    @Test
    public void test(){
        int[] nums = {-1};
        rotate(nums,2);
        System.out.println(Arrays.toString(nums));
        System.out.println(3%2);
    }
}
